import codecs
import random
import numpy as np

dataset=[]

with codecs.open('dataset.txt',"r",encoding='utf=8') as file:
    for line in file:
        dataset.append(line.strip().split(' '))

TRAIN_LEN = int(len(dataset)*0.75)

random.shuffle(dataset)
dataset=np.asarray(dataset)

train_data = np.array(dataset[:TRAIN_LEN, :-1], dtype=float)
train_labels = np.array(dataset[:TRAIN_LEN, -1], dtype=int)

test_data = np.array(dataset[TRAIN_LEN:, :-1], dtype=float)
test_labels = np.array(dataset[TRAIN_LEN:, -1], dtype=int)

mean = train_data.mean(axis=0)
train_data -= mean
std = train_data.std(axis=0)
train_data /= std

with codecs.open('model_notif.txt',"w",encoding='utf-8') as file:
    for t in mean:
        file.write("{:.3f}\n".format(t))
    for t in std:
        file.write("{:.3f}\n".format(t))

test_data -= mean
test_data /= std

def to_one_hot(labels, dimension=3):
    results = np.zeros((len(labels), dimension))
    for i, label in enumerate(labels):
        results[i, label] = 1
    return results

train_labels = to_one_hot(train_labels)
test_labels = to_one_hot(test_labels)

'''y_train = np.array(train_labels)
y_test = np.array(test_labels)'''

from keras import models
from keras import layers
from keras import regularizers

import tensorflow as tf
config = tf.compat.v1.ConfigProto(allow_soft_placement=True)
config.gpu_options.per_process_gpu_memory_fraction = 0.90
config.gpu_options.allow_growth=True
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=config))

def build_model():
    model = models.Sequential()
    model.add(layers.Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.001), input_shape=(35,)))
    #model.add(layers.Dropout(0))
    model.add(layers.Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.001)))
    #model.add(layers.Dropout(0))
    model.add(layers.Dense(3, activation='softmax'))

    model.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])
    return model



# K折验证
'''k = 4
num_val_samples = len(train_data) // k
num_epochs = 20
all_history = []

for i in range(k):
    print('# This is ', i, 'th training.')
    val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]  
    val_targets = train_labels[i * num_val_samples: (i + 1) * num_val_samples]

    partial_train_data = np.concatenate(   
        [train_data[:i * num_val_samples], 
         train_data[(i + 1) * num_val_samples:]],  
        axis=0) 
    partial_train_targets = np.concatenate( 
        [train_labels[:i * num_val_samples], 
         train_labels[(i + 1) * num_val_samples:]],  
        axis=0)
    
    model_ins = build_model()
    history = model_ins.fit(partial_train_data,
                    partial_train_targets,
                    epochs = num_epochs,
                    batch_size = 1,
                    validation_data=(val_data, val_targets))
    
    all_history.append(history.history)

import pylab as plt
loss_values = [np.mean([x['loss'][i] for x in all_history]) for i in range(num_epochs)]
val_loss_values = [np.mean([x['val_loss'][i] for x in all_history]) for i in range(num_epochs)]
epochs = range(1, len(loss_values) + 1)
plt.plot(epochs, loss_values, 'bo', label='Training loss') 
plt.plot(epochs, val_loss_values, 'b', label='Validation loss') 
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

plt.clf()
acc = [np.mean([x['accuracy'][i] for x in all_history]) for i in range(num_epochs)]
val_acc = [np.mean([x['val_accuracy'][i] for x in all_history]) for i in range(num_epochs)]
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()'''

model=build_model()
history = model.fit(train_data,
                train_labels,
                epochs = 3,
                batch_size = 1)

results = model.evaluate(test_data, test_labels)

print(results)

model.save('model.h5', True, save_format='h5')

'''plt.clf()
val_mae = [np.mean([x['val_mean_absolute_error'][i] for x in all_history]) for i in range(num_epochs)]
plt.plot(epochs, val_mae, 'b', label='Validation MAE')
plt.title('Training and Validation MAE')
plt.xlabel('Epochs')
plt.ylabel('MAE')
plt.legend()
plt.show()'''

'''
input('please\n')

model.fit(x_train, y_train, epochs=4, batch_size=512)
results = model.evaluate(x_test, y_test)'''

import code
code.interact(banner = "", local = locals())